package com.mouse.hxsj.view;

import com.melloware.jintellitype.JIntellitype;
import com.mouse.hxsj.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

@Component
public class MainFrm extends JFrame {
    private static boolean trayFlag = true;
    private JPanel contentPane;
    private JDesktopPane desktopPane;
    @Autowired
    private RoleIFrm roleIFrm;
    @Autowired
    private ProcessIFrm processIFrm;
    @Autowired
    private EvocationFrm evocationFrm;
    @Autowired
    private AutoCallFrm autoCallFrm;
    @Autowired
    private AutoKeyFrm autoKeyFrm;
    @Autowired
    private ShopFrm shopFrm;

    @Autowired
    private FamilyFrm familyFrm;
    @Autowired
    private UserFrm userFrm;
    @Autowired
    private BaoFrm baoFrm;
    //定义热键标识，用于在设置多个热键时，在事件处理中区分用户按下的热键
    public static final int HIDEN = 2;
    public static final int SHOW = 1;
    public static final int CLOSE = 0;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrm frame = new MainFrm();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainFrm() {
        setTitle("幻想世界");
        setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        setBounds(100, 100, 1000, 700);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnNewMenu = new JMenu("功能");
        menuBar.add(mnNewMenu);

        JMenu mnNewMenu_login = new JMenu("登录");
        mnNewMenu.add(mnNewMenu_login);

        JMenuItem mntmNewMenuItem_account = new JMenuItem("QQ账号");
        mnNewMenu_login.add(mntmNewMenuItem_account);
        mntmNewMenuItem_account.addActionListener(e -> {
            if (userFrm == null) {
                userFrm = new UserFrm();
            } else {
                userFrm.initFrm();
            }
            userFrm.setVisible(true);
            desktopPane.add(userFrm);
        });


        JMenuItem mntmNewMenuItem = new JMenuItem("游戏角色");
        mntmNewMenuItem.addActionListener(arg0 -> {
            if (roleIFrm == null) {
                roleIFrm = new RoleIFrm();
            } else {
                roleIFrm.initFrm();
            }
            roleIFrm.setVisible(true);
            desktopPane.add(roleIFrm);
        });
        mnNewMenu_login.add(mntmNewMenuItem);

        JMenuItem mntmNewMenuItem_process = new JMenuItem("游戏进程");

        mntmNewMenuItem_process.addActionListener(arg0 -> {
            if (processIFrm == null) {
                processIFrm = new ProcessIFrm();
            } else {
                processIFrm.initFrm();
            }
            processIFrm.setVisible(true);
            desktopPane.add(processIFrm);
        });
        mnNewMenu.add(mntmNewMenuItem_process);

        JMenu mnNewMenu_gn = new JMenu("功能");
        mnNewMenu.add(mnNewMenu_gn);

        JMenuItem mntmNewMenuItem_autoCall = new JMenuItem("自动喊话");
        mnNewMenu_gn.add(mntmNewMenuItem_autoCall);
        mntmNewMenuItem_autoCall.addActionListener(e -> {
            if (autoCallFrm == null) {
                autoCallFrm = new AutoCallFrm();
            } else {
                autoCallFrm.initFrm();
            }
            autoCallFrm.setVisible(true);
            desktopPane.add(autoCallFrm);
        });

        JMenuItem mntmNewMenuItem_evocationFrm = new JMenuItem("招魂挂机");
        mnNewMenu_gn.add(mntmNewMenuItem_evocationFrm);
        mntmNewMenuItem_evocationFrm.addActionListener(e -> {
            if (evocationFrm == null) {
                evocationFrm = new EvocationFrm();
            } else {
                evocationFrm.initFrm();
            }
            evocationFrm.setVisible(true);
            desktopPane.add(evocationFrm);
        });

        JMenuItem mntmNewMenuItem_autoKey = new JMenuItem("自动按键");
        mnNewMenu_gn.add(mntmNewMenuItem_autoKey);
        mntmNewMenuItem_autoKey.addActionListener(e -> {
            if (autoKeyFrm == null) {
                autoKeyFrm = new AutoKeyFrm();
            } else {
                autoKeyFrm.initFrm();
            }
            autoKeyFrm.setVisible(true);
            desktopPane.add(autoKeyFrm);
        });

        JMenuItem mntmNewMenuItem_shop = new JMenuItem("自动购物");

        mntmNewMenuItem_shop.addActionListener(arg0 -> {
            if (shopFrm == null) {
                shopFrm = new ShopFrm();
            } else {
                shopFrm.initFrm();
            }
            shopFrm.setVisible(true);
            desktopPane.add(shopFrm);
        });
        mnNewMenu.add(mntmNewMenuItem_shop);
        //
        JMenuItem mntmNewMenuItem_family = new JMenuItem("家族领地");

        mntmNewMenuItem_family.addActionListener(arg0 -> {
            if (familyFrm == null) {
                familyFrm = new FamilyFrm();
            } else {
                familyFrm.initFrm();
            }
            familyFrm.setVisible(true);
            desktopPane.add(familyFrm);
        });
        mnNewMenu.add(mntmNewMenuItem_family);

        JMenuItem mntmNewMenuItem_bao = new JMenuItem("等宝");

        mntmNewMenuItem_bao.addActionListener(arg0 -> {
            if (baoFrm == null) {
                baoFrm = new BaoFrm();
            } else {
                baoFrm.initFrm();
            }
            baoFrm.setVisible(true);
            desktopPane.add(baoFrm);
        });
        mnNewMenu.add(mntmNewMenuItem_bao);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        desktopPane = new JDesktopPane();
        contentPane.add(desktopPane, BorderLayout.CENTER);
        // 设置JFrame最大化
//		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        //托盘
        setSystemTray();
        try {
//            setHotKey();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setHotKey() {
        Config.initDll();
        JIntellitype.getInstance().registerHotKey(SHOW, JIntellitype.MOD_ALT, (int) 'S');
        JIntellitype.getInstance().registerHotKey(CLOSE, JIntellitype.MOD_ALT, (int) 'Q');
        JIntellitype.getInstance().registerHotKey(HIDEN, JIntellitype.MOD_ALT, (int) 'D');
        //第二步：添加热键监听器
        JIntellitype.getInstance().addHotKeyListener(markCode -> {
            switch (markCode) {
                case SHOW:
                    MainFrm.this.setVisible(true);
                    break;
                case CLOSE:
                    int result = JOptionPane.showConfirmDialog(null, "退出系统?");
                    if (result == 0) System.exit(0);
                    break;
                case HIDEN:
                    MainFrm.this.setVisible(false);
                    break;
            }
        });
    }

    private void setSystemTray() {
        if (SystemTray.isSupported()) {
            // 获取当前平台的系统托盘
            SystemTray tray = SystemTray.getSystemTray();

            // 加载一个图片用于托盘图标的显示

            Image image = Toolkit.getDefaultToolkit().getImage(MainFrm.class.getResource("/static/tray.png"));

            // 创建点击图标时的弹出菜单
            PopupMenu popupMenu = new PopupMenu();

            MenuItem openItem = new MenuItem("打开");
            MenuItem exitItem = new MenuItem("退出");

            openItem.addActionListener(e -> {
                // 点击打开菜单时显示窗口
                if (!MainFrm.this.isShowing()) {
                    MainFrm.this.setVisible(true);
                }
            });
            exitItem.addActionListener(e -> {
                // 点击退出菜单时退出程序
                System.exit(0);
            });

            popupMenu.add(openItem);
            popupMenu.add(exitItem);

            // 创建一个托盘图标
            TrayIcon trayIcon = new TrayIcon(image, "幻想世界", popupMenu);

            // 托盘图标自适应尺寸
            trayIcon.setImageAutoSize(true);

            trayIcon.addActionListener(e -> System.out.println("托盘图标被右键点击"));
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    switch (e.getButton()) {
                        case MouseEvent.BUTTON1: {
                            System.out.println("托盘图标被鼠标左键被点击");
                            if (!MainFrm.this.isShowing()) {
                                MainFrm.this.setVisible(true);
                            }
                            break;
                        }
                        case MouseEvent.BUTTON2: {
                            System.out.println("托盘图标被鼠标中键被点击");
                            break;
                        }
                        case MouseEvent.BUTTON3: {
                            System.out.println("托盘图标被鼠标右键被点击");
                            break;
                        }
                        default: {
                            break;
                        }
                    }
                }
            });

            // 添加托盘图标到系统托盘
            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                e.printStackTrace();
            }

        } else {
            trayFlag = false;
            System.out.println("当前系统不支持系统托盘");
        }
    }
}
